<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>广告修改</title>
		<link rel="stylesheet" href="../../../component/pear/css/pear.css" />
	</head>
	<body>
		<form class="layui-form" action="">
			<div class="mainBox">
				<div class="main-container">
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">编码</label>
						<div class="layui-input-inline">
							<input type="text" name="code" placeholder="" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">名称</label>
						<div class="layui-input-inline">
							<input type="text" name="name" placeholder="" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item layui-inline">
						<label class="layui-form-label">上级</label>
						<div class="layui-input-inline">
							<select id="parentSelect" lay-filter="province" name="parentid" lay-search>
								<option value="">请选择</option>
							</select>
						</div>
					</div>
					<div class="layui-form-item layui-inline layui-hide">
						<label class="layui-form-label">位置</label>
						<div class="layui-input-inline">
							<input type="text" name="pos" placeholder="" value="31.82°,117.22°" class="layui-input">
						</div>
					</div>


				</div>
			</div>
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
						lay-filter="user-save">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>
		<script src="../../../component/layui/layui.js"></script>
		<script src="../../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery','upload'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				var upload = layui.upload;
				var id=sessionStorage.getItem("id")
				$.ajaxSetup({
					headers: {
						'Authorization': window.sessionStorage.getItem("token")
					}
				})

				// $("[name='pos']").val("");

				$.ajax({
					url: window.BASEPATH + 'city/selectById/0', // 替换成实际的数据接口URL
					type: 'GET',
					dataType: 'json',
					success: function (response) {
						if (response.code === 0) {
							var data = response.data;

							// 清空下拉框中的选项
							$('#parentSelect').empty();
							// 添加默认的"请选择"选项
							$('#parentSelect').append('<option value="">请选择</option>');
							// 遍历数据，将每个选项添加到下拉框中
							for (var i = 0; i < data.length; i++) {
								var option = new Option(data[i].name, data[i].id);
								$('#parentSelect').append(option);
							}
							// 手动重新渲染下拉框
							form.render('select');
							performSecondAjaxRequest();
						} else {
							// 处理返回的错误信息
						}
					},
					error: function () {
						// 处理请求错误的情况
					}
				});


				function performSecondAjaxRequest() {
					$.getJSON(window.BASEPATH + "city/select/" +id,
							function (res) {
								if (res.code != 0)
									return layer.msg('信息检索失败');
								let data = res.data;
								$("[name='code']").val(data.code);
								$("[name='name']").val(data.name);

								if (data.parentid === 0) {
									$("#parentSelect").parent().parent().hide();
								}
								$("[name='parentid']").val(data.parentid);
								$("[name='pos']").val(data.pos);
								// $('#preview').attr('src', url2+data.imgurl);
								form.render();

							});
				}


				form.on('submit(user-save)', function (data) {
					data.field.id = sessionStorage.getItem("id");
					$.ajax({
						url: window.BASEPATH + 'city/edit',
						data: data.field,
						type: 'post',
						success: function (result) {
							if (result.code == 0) {
								layer.msg(result.msg, {
									icon: 1,
									time: 1000
								}, function () {
									parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
									parent.layui.table.reload("user-table");
								});
							} else {
								layer.msg(result.msg, {
									icon: 2,
									time: 1000
								});
							}
						}
					})
					return false;
				})
			})
		</script>
		<script>
		</script>
	</body>
</html>
